package com.zhang.third.day03;

import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple5;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.util.Collector;

import java.util.Random;

/**
 * @title: KeyedProcessFunction一次性求最大值、最小值、总和、总条数、平均值
 * @author: zhang
 * @date: 2022/4/6 15:11
 */
public class Example10 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new SourceFunction<Integer>() {
                    private boolean running = true;
                    private Random random = new Random();

                    @Override
                    public void run(SourceContext<Integer> ctx) throws Exception {
                        while (running) {
                            ctx.collect(random.nextInt(1000));
                            Thread.sleep(100L);
                        }
                    }

                    @Override
                    public void cancel() {
                        running = false;
                    }
                })
                .keyBy(r -> 1)
                .process(new KeyedProcessFunction<Integer, Integer, String>() {
                    private ValueState<Tuple5<Integer, Integer, Integer, Integer, Integer>> accumulator;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        accumulator = getRuntimeContext().getState(
                                new ValueStateDescriptor<Tuple5<Integer, Integer, Integer, Integer, Integer>>(
                                        "accumulator",
                                        Types.TUPLE(Types.INT, Types.INT, Types.INT, Types.INT, Types.INT)
                                )
                        );
                    }

                    @Override
                    public void processElement(Integer value, KeyedProcessFunction<Integer, Integer, String>.Context ctx, Collector<String> out) throws Exception {
                        if (accumulator.value() == null) {
                            accumulator.update(Tuple5.of(
                                    value,
                                    value,
                                    value,
                                    1,
                                    value
                            ));
                        } else {
                            Tuple5<Integer, Integer, Integer, Integer, Integer> tmp = accumulator.value();
                            accumulator.update(Tuple5.of(
                                    Math.min(value, tmp.f0),          // 聚合最小值
                                    Math.max(value, tmp.f1),          // 聚合最大值
                                    value + tmp.f2,                   // 聚合总和
                                    tmp.f3 + 1,                       // 聚合总数
                                    (value + tmp.f2) / (tmp.f3 + 1)   // 聚合平均值
                            ));
                        }
                        out.collect("最小值：" + accumulator.value().f0 + "" +
                                ";最大值：" + accumulator.value().f1 + "" +
                                ";总和：" + accumulator.value().f2 + "" +
                                ";总条数：" + accumulator.value().f3 + "" +
                                ";平均值：" + accumulator.value().f4);
                    }
                })
                .print();

        env.execute();
    }
}
